import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { useUserStore } from '@/stores'
NProgress.configure({ showSpinner: false })

// vue3中使用函数创建路由对象
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL), // 路由模式（hash、history）
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: 'home',
          component: () => import('@/views/home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: 'article',
          component: () => import('@/views/article/index.vue'),
          meta: {
            title: '健康百科'
          }
        },
        {
          path: 'notice',
          component: () => import('@/views/notice/index.vue'),
          meta: {
            title: '消息中心'
          }
        },
        {
          path: 'user',
          component: () => import('@/views/mine/index.vue'),
          meta: {
            title: '个人中心'
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/login/callback',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('@/views/login/login-callback.vue'),
      meta: {
        title: '手机号绑定'
      }
    },
    {
      path: '/user/patient',
      component: () => import('@/views/mine/patient-page.vue'),
      meta: {
        title: '家庭档案'
      }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/consult/consult-fast.vue'),
      meta: {
        title: '极速问诊'
      }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/consult/consult-dep.vue'),
      meta: {
        title: '选择科室'
      }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/consult/consult-illness.vue'),
      meta: {
        title: '图文问诊'
      }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/consult/consult-pay.vue'),
      meta: {
        title: '支付'
      }
    },
    {
      path: '/room',
      component: () => import('@/views/room/index.vue'),
      meta: {
        title: '医生问诊室'
      },
      beforeEnter (to) {
        if (to.query.payResult === 'false') return '/user/consult'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/mine/consult-page.vue'),
      meta: {
        title: '问诊记录'
      }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/order/pay.vue'),
      meta: {
        title: '药品支付'
      }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/order/detail.vue'),
      meta: {
        title: '药品订单详情'
      }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/order/logistics.vue'),
      meta: {
        title: '物流轨迹'
      }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/mine/consult-detail.vue'),
      meta: {
        title: '问诊详情'
      }
    },
    {
      path: '/test',
      component: () => import('@/views/test/parent.vue'),
      meta: {
        title: '测试'
      }
    },
    {
      path: '/mock',
      component: () => import('@/views/test/test-mock.vue'),
      meta: {
        title: 'mock的使用'
      }
    },
    {
      path: '/map',
      component: () => import('@/views/test/map.vue'),
      meta: {
        title: '地图'
      }
    }
  ]
})

// 导航守卫-前置守卫
const whiteList = ['/login', '/login/callback']
router.beforeEach(to => {
  NProgress.start()
  // next()
  const store = useUserStore()

  // 如果没有登录且不在白名单内，去登录
  if (!store.user?.token && !whiteList.includes(to.path)) return '/login'
  // 否则不做任何处理
})

// 导航守卫-后置守卫
router.afterEach(to => {
  document.title = `优医问诊-${to.meta.title}`
  NProgress.done()
})

export default router
